/**
 * Copyright (c) 2009, http://www.softwareinlife.com. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided that 
 * the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the http://www.softwareinlife.com nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE SOFTWAREINLIFE AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.softwareinlife.vision.android.ux.init;

import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.softwareinlife.vision.android.R;
import com.softwareinlife.vision.android.data.dao.UserDAO;
import com.softwareinlife.vision.android.data.exceptions.DatabaseConnectionException;
import com.softwareinlife.vision.android.data.exceptions.UserCreateException;
import com.softwareinlife.vision.android.data.model.UserModel;
import com.softwareinlife.vision.android.system.util.StringUtil;
import com.softwareinlife.vision.android.ux.util.SharedPreferenceUtility;

/**
 * Account Activity
 * 
 * @author Jang, Sun-Jin(jangsunjin@softwareinlife.com)
 * @date 2009. 10. 10.
 */
public class AccountActivity extends Activity {

	/**
	 * Account ID
	 */
	private EditText accountIDEditText = null;

	/**
	 * Account Password
	 */
	private EditText accountPasswordEditText = null;

	/**
	 * Account Password Confirm
	 */
	private EditText accountPasswordConfirmEditText = null;

	/**
	 * Account First Name
	 */
	private EditText accountFirstNameEditText = null;

	/**
	 * Account Last Name
	 */
	private EditText accountLastNameEditText = null;

	/**
	 * Account Nick Name
	 */
	private EditText accountNickNameEditText = null;

	/**
	 * Account Email
	 */
	private EditText accountEMailEditText = null;

	/**
	 * Button of Create
	 */
	private Button createButton = null;

	/**
	 * Button of Rest
	 */
	private Button resetButton = null;

	/**
	 * Not Equal Password
	 */
	private final int NOT_EQUAL_PASSWORD = 1;

	/**
	 * Please Input Field
	 */
	private final int PLZ_INPUT_FIELD = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Set View
		setContentView(R.layout.init_account);

		// Initialize Members
		initMemembers();
	}

	/**
	 * Initialize Members
	 */
	private void initMemembers() {
		// Edit Text
		accountIDEditText = (EditText) findViewById(R.id.accountIdEditText);
		accountPasswordEditText = (EditText) findViewById(R.id.accountPasswordEditText);
		accountPasswordConfirmEditText = (EditText) findViewById(R.id.accountPasswordConfirmEditText);
		accountFirstNameEditText = (EditText) findViewById(R.id.accountFirstNameEditText);
		accountLastNameEditText = (EditText) findViewById(R.id.accountLastNameEditText);
		accountNickNameEditText = (EditText) findViewById(R.id.accountNickNameEditText);
		accountEMailEditText = (EditText) findViewById(R.id.accountEmailEditText);

		// CREATE BUTTON //////////////////////////////////
		createButton = (Button) findViewById(R.id.createButton);
		createButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				createData();
			}
		});

		// Reset BUTTON //////////////////////////////////
		resetButton = (Button) findViewById(R.id.resetButton);
		resetButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				resetData();
			}
		});
	}

	/**
	 * Create Data
	 */
	protected void createData() {
		// Check Parameters
		if ((accountIDEditText != null && accountIDEditText.getText() != null && accountIDEditText
				.getText().length() > 3)
				&& (accountPasswordEditText != null
						&& accountPasswordEditText.getText() != null && accountPasswordEditText
						.getText().length() > 3)
				&& (accountPasswordConfirmEditText != null
						&& accountPasswordConfirmEditText.getText() != null && accountPasswordConfirmEditText
						.getText().length() > 3)) {

			// Check Passwords
			if (accountPasswordEditText.getText().equals(
					accountPasswordConfirmEditText.getText())) {
				showDialog(NOT_EQUAL_PASSWORD);
			}

			// User
			String userId = StringUtil.toString(accountIDEditText.getText());
			String userPassword = StringUtil.toString(accountPasswordEditText
					.getText());
			String userFirstName = StringUtil.toString(accountFirstNameEditText
					.getText());
			String userLastName = StringUtil.toString(accountLastNameEditText
					.getText());
			String userNickName = StringUtil.toString(accountNickNameEditText
					.getText());
			String userEmail = StringUtil.toString(accountEMailEditText
					.getText());

			// Create User Model
			UserModel user = new UserModel();
			user.setUserId(userId);
			user.setUserPassword(userPassword);
			user.setFirstName(userFirstName);
			user.setLastName(userLastName);
			user.setNickName(userNickName);
			user.setEMail(userEmail);
			
			Date now = new Date();
			user.setCreatedDate(now);
			user.setModifiedDate(now);

			// Insert to DB
			UserDAO dao = null;
			try {
				dao = new UserDAO(getApplicationContext());
				dao.create(user);
				
				// Shared Preferences
				SharedPreferenceUtility.setUserId(getApplicationContext(), userId);
			} catch (UserCreateException e) {
				e.printStackTrace();
			} catch (DatabaseConnectionException e) {
				e.printStackTrace();
			}
			dao.close();

			// Change Activity
			Intent intent = new Intent(this, TypeActivity.class);
			startActivity(intent);
		} else {
			showDialog(PLZ_INPUT_FIELD);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// Dialog
		Dialog dialog = null;

		// Builder
		Builder builder = null;

		switch (id) {
		case NOT_EQUAL_PASSWORD:
			// Create Builder
			builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.account_not_eqal_password);

			// Create OK
			builder.setPositiveButton(R.string.common_ok,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {

						}
					});

			dialog = builder.create();
			break;

		case PLZ_INPUT_FIELD:
			// Create Builder
			builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.account_plz_input_field);

			// Create OK
			builder.setPositiveButton(R.string.common_ok,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {

						}
					});

			dialog = builder.create();
			break;
		}

		return dialog;
	}

	/**
	 * Reset Data
	 */
	protected void resetData() {
		accountIDEditText.setText("");
		accountPasswordEditText.setText("");
		accountPasswordConfirmEditText.setText("");
		accountFirstNameEditText.setText("");
		accountLastNameEditText.setText("");
		accountNickNameEditText.setText("");
		accountEMailEditText.setText("");
	}
}
